package com.wutianyi.servlet;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.wutianyi.utils.HtmlUtils;

public class FetchServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3926779648582991923L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String url = req.getParameter("url");
		if (StringUtils.isBlank(url)) {
			return;
		}
		url = URLDecoder.decode(url, "utf-8");
		String encoding = req.getParameter("encoding");
		if (StringUtils.isBlank(encoding)) {
			encoding = "utf-8";
		}
		String name = req.getParameter("name");

		String content = HtmlUtils.getBodyString(url, encoding);
		Entity entity = new Entity("rss", name);
		entity.setProperty("content", new Text(content));
		DatastoreServiceFactory.getDatastoreService().put(entity);

	}

}
